package cn.edu.pku.ss.music;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import cn.edu.pku.ss.music.activities.PortalActivity;
import cn.edu.pku.ss.music.util.ImageUtil;
import cn.edu.pku.ss.music.util.Util;
import cn.jpush.android.api.InstrumentedActivity;

public class MainActivity extends InstrumentedActivity {
	private TextView receive_textview;
	private ImageView logo;

	public static boolean isForeground = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.splash_layout);
		
		logo = (ImageView) findViewById(R.id.logo);
		Bitmap bitmap = ImageUtil.drawableToBitmap(logo.getDrawable());  
		logo.setImageBitmap(ImageUtil.createReflectionImageWithOrigin(bitmap));
//		receive_textview = (TextView) findViewById(R.id.receive_textview);
		
//��ʱ������������Ϣ
//		JPushInterface.init(this);
//		registerMessageReceiver();
		
//		Intent intent = new Intent(this, WelcomeActivity.class);
		Intent intent = new Intent(this, PortalActivity.class);
		startActivity(intent);
		
	}

	// for receive customer msg from jpush server
	private MessageReceiver mMessageReceiver;
	public static final String MESSAGE_RECEIVED_ACTION = "com.example.jpushdemo.MESSAGE_RECEIVED_ACTION";
	public static final String KEY_TITLE = "title";
	public static final String KEY_MESSAGE = "message";
	public static final String KEY_EXTRAS = "extras";

	public void registerMessageReceiver() {
		mMessageReceiver = new MessageReceiver();
		IntentFilter filter = new IntentFilter();
		filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
		filter.addAction(MESSAGE_RECEIVED_ACTION);
		registerReceiver(mMessageReceiver, filter);
	}

	public class MessageReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
				String messge = intent.getStringExtra(KEY_MESSAGE);
				String extras = intent.getStringExtra(KEY_EXTRAS);
				StringBuilder showMsg = new StringBuilder();
				showMsg.append(KEY_MESSAGE + " : " + messge + "\n");
				if (!Util.isEmpty(extras)) {
					showMsg.append(KEY_EXTRAS + " : " + extras + "\n");
				}
				setCostomMsg(showMsg.toString());
			}
		}
	}

	private void setCostomMsg(String msg) {
		if (null != receive_textview) {
			receive_textview.setText(msg);
			receive_textview.setVisibility(android.view.View.VISIBLE);
		}
	}

	@Override
	protected void onResume() {
		isForeground = true;
		super.onResume();
	}

	@Override
	protected void onPause() {
		isForeground = false;
		super.onPause();
	}
}
